home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 343_02 / rotate.c < prev    next >
C/C++ Source or Header  |  1992-04-13  |  7KB  |  234 lines

  1.  
  2.  
  3.        /***********************************************
  4.        *
  5.        *       file d:\cips\rotate.c
  6.        *
  7.        *       Functions: This file contains
  8.        *          rotate_flip_image_array
  9.        *
  10.        *       Purpose:
  11.        *          This function rotates or flips an image
  12.        *          array in one of five ways.
  13.        *
  14.        *       External Calls:
  15.        *          wtiff.c - does_not_exist
  16.        *                    round_off_image_size
  17.        *                    create_allocate_tiff_file
  18.        *                    write_array_into_tiff_image
  19.        *          tiff.c - read_tiff_header
  20.        *          rtiff.c - read_tiff_image
  21.        *
  22.        *
  23.        *       Modifications:
  24.        *          1 April 1992 - created
  25.        *
  26.        *************************************************/
  27.  
  28. #include "d:\cips\cips.h"
  29.  
  30.      /*******************************************
  31.      *
  32.      *   rotate_flip_image_array(...
  33.      *
  34.      *   This function rotates an image array
  35.      *   in one of three ways or flips an image
  36.      *   array either vertically or horizontally.
  37.      *   The rotation_type parameter specifies
  38.      *   the operation.  When rotation_type is 
  39.      *   1, 2, or 3 you rotate.  When it is
  40.      *   4 or 5 you flip.
  41.      *
  42.      *   I define rotation as this:  Pin down the
  43.      *   lower left hand corner of the image array
  44.      *   and rotate the image 90 degrees clockwise.
  45.      *   1 rotation is 90 degrees, 2 rotations are
  46.      *   180 degrees, and 3 rotations are 270 degrees.
  47.      *   4 rotations bring you back to where you
  48.      *   started.
  49.      *
  50.      *   The cases are:
  51.      *
  52.      *   If the input image array is:
  53.      *        1 2 3
  54.      *        4 5 6
  55.      *        7 8 9
  56.      *
  57.      *   Rotate # 1 - the result is:
  58.      *        7 4 1
  59.      *        8 5 2
  60.      *        9 6 3
  61.      *
  62.      *   Rotate # 2 - the result is:
  63.      *        9 8 7
  64.      *        6 5 4
  65.      *        3 2 1
  66.      *
  67.      *   Rotate # 3 - the result is:
  68.      *        3 6 9
  69.      *        2 5 8
  70.      *        1 4 7
  71.      *
  72.      *   Flip # 4 - horizontal the result is:
  73.      *        3 2 1
  74.      *        6 5 4
  75.      *        9 8 7
  76.      *
  77.      *   Flip # 5 - vertical the result is:
  78.      *        7 8 9
  79.      *        4 5 6
  80.      *        1 2 3
  81.      *
  82.      *
  83.      *   The in_file is the source image with
  84.      *   parameters given by il1, ie1, ll1, le1.
  85.      *
  86.      *   The out_file is the destination image with
  87.      *   parameters given by il2, ie2, ll2, le2.
  88.      *
  89.      *******************************************/
  90.  
  91.  
  92. rotate_flip_image_array(in_name, out_name, the_image,
  93.                    out_image, il1, ie1, ll1, le1,
  94.                    il2, ie2, ll2, le2, rotation_type)
  95.    char  in_name[], out_name[];
  96.    int   il1, ie1, ll1, le1,
  97.          il2, ie2, ll2, le2, rotation_type;
  98.    short the_image[ROWS][COLS],
  99.          out_image[ROWS][COLS];
  100. {
  101.    int    cd2, i, j, length, rd2, type, width;
  102.    struct tiff_header_struct image_header;
  103.  
  104.    if(does_not_exist(out_name)){
  105.       printf("\n\n output file does not exist %s", out_name);
  106.       read_tiff_header(in_name, &image_header);
  107.       round_off_image_size(&image_header,
  108.                            &length, &width);
  109.       image_header.image_length = length*ROWS;
  110.       image_header.image_width  = width*COLS;
  111.       create_allocate_tiff_file(out_name, &image_header,
  112.                                 out_image);
  113.    }  /* ends if does_not_exist */
  114.  
  115.      /*******************************************
  116.      *
  117.      *   Check the rotation_type.  If it is not
  118.      *   a valid value, set it to 1.
  119.      *
  120.      *******************************************/
  121.  
  122.    type = rotation_type;
  123.    if(type != 1  &&
  124.       type != 2  &&
  125.       type != 3  &&
  126.       type != 4  &&
  127.       type != 5) type = 1;
  128.  
  129.    read_tiff_image(in_name, the_image, il1, ie1, ll1, le1);
  130.  
  131.      /*******************************************
  132.      *
  133.      *   Rotate the image array as desired.
  134.      *
  135.      *******************************************/
  136.  
  137.      /*******************************************
  138.      *
  139.      *   1 90 degree rotation
  140.      *
  141.      *******************************************/
  142.  
  143.    if(type == 1  ||  type == 2  ||  type == 3){
  144.       for(i=0; i<ROWS; i++){
  145.          for(j=0; j<COLS; j++)
  146.             out_image[j][COLS-1-i] = the_image[i][j];
  147.       }  /* ends loop over i */
  148.    }  /* ends if type == 1 or 2 or 3 */
  149.  
  150.  
  151.      /*******************************************
  152.      *
  153.      *   a second 90 degree rotation
  154.      *
  155.      *******************************************/
  156.  
  157.    if(type == 2  ||  type == 3){
  158.       for(i=0; i<ROWS; i++)
  159.          for(j=0; j<COLS; j++)
  160.             the_image[i][j] = out_image[i][j];
  161.       for(i=0; i<ROWS; i++){
  162.          for(j=0; j<COLS; j++)
  163.             out_image[j][COLS-1-i] = the_image[i][j];
  164.       }  /* ends loop over i */
  165.    }  /* ends if type == 2 or 3 */
  166.  
  167.  
  168.      /*******************************************
  169.      *
  170.      *   a third 90 degree rotation
  171.      *
  172.      *******************************************/
  173.  
  174.    if(type == 3){
  175.       for(i=0; i<ROWS; i++)
  176.          for(j=0; j<COLS; j++)
  177.             the_image[i][j] = out_image[i][j];
  178.       for(i=0; i<ROWS; i++){
  179.          for(j=0; j<COLS; j++)
  180.             out_image[j][COLS-1-i] = the_image[i][j];
  181.       }  /* ends loop over i */
  182.    }  /* ends if type == 3 */
  183.  
  184.  
  185.      /*******************************************
  186.      *
  187.      *   Flip the image array horizontally
  188.      *   about the center vertical axis.
  189.      *
  190.      *******************************************/
  191.  
  192.    if(type == 4){
  193.       cd2 = COLS/2;
  194.       for(j=0; j<cd2; j++){
  195.          for(i=0; i<ROWS; i++){
  196.             out_image[i][COLS-1-j] = the_image[i][j];
  197.          }  /* ends loop over i */
  198.       }  /* ends loop over j */
  199.  
  200.       for(j=cd2; j<COLS; j++){
  201.          for(i=0; i<ROWS; i++){
  202.             out_image[i][COLS-1-j] = the_image[i][j];
  203.          }  /* ends loop over i */
  204.       }  /* ends loop over j */
  205.    }  /* ends if type == 4 */
  206.  
  207.  
  208.      /*******************************************
  209.      *
  210.      *   Flip the image array vertically
  211.      *   about the center horizontal axis.
  212.      *
  213.      *******************************************/
  214.  
  215.    if(type == 5){
  216.       rd2 = ROWS/2;
  217.       for(i=0; i<rd2; i++){
  218.          for(j=0; j<COLS; j++){
  219.             out_image[ROWS-1-i][j] = the_image[i][j];
  220.          }  /* ends loop over j */
  221.       }  /* ends loop over i */
  222.  
  223.       for(i=rd2; i<ROWS; i++){
  224.          for(j=0; j<COLS; j++){
  225.             out_image[ROWS-1-i][j] = the_image[i][j];
  226.          }  /* ends loop over j */
  227.       }  /* ends loop over i */
  228.    }  /* ends if type == 5 */
  229.  
  230.    write_array_into_tiff_image(out_name, out_image,
  231.                                il2, ie2, ll2, le2);
  232.  
  233. }  /* ends rotate_flip_image_array */
  234.